package sync.model.auto;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

import net.sf.json.JSONArray;

import org.apache.commons.lang.RandomStringUtils;

/**
 * 数据集
 * 
 * @author lili
 * 
 */
public class DataBase {
	/*
	 * 远程仓库，假定为有锁机制
	 */
	public static final ConcurrentHashMap<String, RemoteEntity> remoteDB = new ConcurrentHashMap<String, RemoteEntity>();
	/*
	 * 本地库，无锁机制
	 */
	public static final Map<String, LocalEntity> localDB = new HashMap<String, LocalEntity>();
	/*
	 * 等待上传的缓存库，线程安全的阻塞机制
	 */
	public static final ArrayBlockingQueue<LocalEntity> cache = new ArrayBlockingQueue<LocalEntity>(
			10, true);

	/*
	 * 手动提交请求计数器
	 */
	public static final AtomicInteger upCount = new AtomicInteger(0);

	/*
	 * 初始化库，远程和本地为已同步状态，数据一致
	 */
	static {
		System.out.println("...数据初始化...");
		for (int i = 0; i < 100; i++) {
			String value = RandomStringUtils.randomNumeric(3);
			remoteDB.put(i + "", new RemoteEntity(i + "", value));
			localDB.put(i + "", new LocalEntity(i + "", i + "", value));
		}
	}

	// public static void main(String[] args) {
	// ExecutorService exe = Executors.newFixedThreadPool(5);
	// for (int i = 0; i < 5; i++) {
	// exe.execute(new Thread() {
	// @Override
	// public void run() {
	// System.out.println(JSONArray.fromObject(DataBase.remoteDB));
	// }
	// });
	// }
	// exe.shutdown();
	// }
}
